package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("login")
    @ApiOperation("用户登录")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam) {
        log.debug("用户登录,userLoginParam={}", userLoginParam);
        //Ctrl点方法名,去到接口  Ctrl+Alt去到接口实现类
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }

    @PostMapping("save")
    @ApiOperation("新增用户")
    public JsonResult saveUser(UserSaveParam userSaveParam) {
        log.debug("保存用户，userSaveParam={}", userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select")
    @ApiOperation("查询用户")
    public JsonResult selectUser(UserQuery userQuery) {
        log.debug("查询用户，userQuery={}", userQuery);
        List<UserVO> list = userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("/update/status/{userId}/{status}")
    @ApiOperation("更新用户状态")
    public JsonResult updateStatus(@PathVariable Long userId, @PathVariable String status) {
        log.debug("修改用户状态，userId={},status={}", userId, status);
        userService.updateStatus(userId, status);
        return JsonResult.ok();
    }

    @PostMapping("/update/password/{userId}")
    @ApiOperation("重置密码")
    public JsonResult resetPassword(@PathVariable Long userId) {
        log.debug("重置密码，userId={}", userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }

    @PostMapping("delete/{userId}")
    @ApiOperation("删除用户")
    public JsonResult deleteUser(@PathVariable Long userId) {
        log.debug("删除用户，userId={}", userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }

    @GetMapping("select/audit/{parentId}")
    @ApiOperation("查询审批人列表")
    public JsonResult selectAuditList(@PathVariable Long parentId) {
        log.debug("查询审批人列表，parentId={}", parentId);
        List<UserVO> list = userService.selectAuditList(parentId);
        return JsonResult.ok(list);
    }

}
